package com.example.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.example.common.enums.ErrorCodeEnum;
import com.example.common.Result;
import com.example.common.enums.RoleEnum;
import com.example.exception.CustomException;
import com.example.pojo.entity.Account;
import com.example.pojo.entity.PetShop;
import com.example.pojo.entity.User;
import com.example.service.AdminService;
import com.example.service.PetShopService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 登录、注册接口
 */
@RestController
public class WebController {

    @Resource
    private UserService userService;

    @Resource
    private PetShopService petShopService;

    @Resource
    private AdminService adminService;

    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        // 1.校验参数
        if (account == null) {
            throw new CustomException(ErrorCodeEnum.PARAM_ERROR);
        }
        String role = account.getRole();
        if (StrUtil.isBlank(role)) {
            throw new CustomException(ErrorCodeEnum.ROLE_LOST_ERROR);
        }
        // 2.根据角色登录
        Account loginAccount;
        if (RoleEnum.USER.name().equals(role)) {
            // 2.1 登录普通用户账号
            loginAccount = this.userService.login(account);
        } else if (RoleEnum.PETSHOP.name().equals(role)) {
            // 2.2 登录宠物店账号
            loginAccount = this.petShopService.login(account);
        } else if (RoleEnum.ADMIN.name().equals(role)) {
            // 2.3 登录管理员账号
            loginAccount = this.adminService.login(account);
        } else {
            // 2.4 其他角色，抛异常
            throw new CustomException(ErrorCodeEnum.ROLE_LOGIN_ERROR);
        }
        return Result.success(loginAccount);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Account account) {
        // 1.校验参数
        if (account == null) {
            throw new CustomException(ErrorCodeEnum.PARAM_LOST_ERROR);
        }
        String role = account.getRole();
        if (StrUtil.isBlank(role)) {
            throw new CustomException(ErrorCodeEnum.ROLE_LOST_ERROR);
        }
        // 2.根据角色注册账号
        if (RoleEnum.USER.name().equals(role)) {
            // 注册普通用户账号
            User user = BeanUtil.copyProperties(account, User.class);
            this.userService.add(user);
        } else if (RoleEnum.PETSHOP.name().equals(role)) {
            // 注册宠物店账号
            PetShop petShop = BeanUtil.copyProperties(account, PetShop.class);
            this.petShopService.add(petShop);
        } else {
            // 其他角色，抛异常
            throw new CustomException(ErrorCodeEnum.ROLE_REGISTER_ERROR);
        }
        return Result.success();
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if (RoleEnum.USER.name().equals(account.getRole())) {
            // 1.修改普通用户账号的密码
            this.userService.updatePassword(account);
        } else if (RoleEnum.PETSHOP.name().equals(account.getRole())) {
            // 2.修改宠物店账号的密码
            this.petShopService.updatePassword(account);
        } else if (RoleEnum.ADMIN.name().equals(account.getRole())) {
            // 3.修改管理员账号的密码
            this.adminService.updatePassword(account);
        }
        return Result.success();
    }
}
